package view.parameterpanels;

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import view.AlphaTextField;
import view.CenterTabbedPane.EBenchmarkTab;
import view.EBenchmarkPanel;
import controller.CtrInformationPanel;

//#Neu
/**
* To draw a parameter panel for input parameters of type location, company, 
* industrial sector or product parameters. The user can add and remove locations, 
* companies, industrial sectors and products and assign a name to each of them.
* @author Sebastian Perlwitz
*
*/
public class LocComIndProdParameterPanel extends EBenchmarkPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The text fields that allow the user to assign a name to a location, 
	 * company or industrial sector.
	 */
	private List<AlphaTextField> nameTextFields;
	
	/**
	 * A button to add a location, company or industrial sector.
	 */
	private JButton btnAdd;
	
	/**
	 * A button to remove a location, company or industrial sector.
	 */
	private JButton btnRemove;
	
	/**
	 * A JPanel where the user can setup what is shown on the panel.
	 */
	private JPanel settings;
	
	/**
	 * The most important part of a ParameterPanel. On this panel
	 * all important parameter information is drawn.
	 */
	private JPanel paramPanel;
	
	/**
	 * The parameter panels get stored in here.
	 */
	private List<JPanel> parameterPanels;
	
	/**
	 * The eBenchmarkTab of this view.
	 */
	private EBenchmarkTab eBenchmarkTab;
	
    /**
	 * To create a new {@code LocationParameterPanel}.
	 * @param infoPanelCtr the controller of the information panel.
	 * @param eBenchmarkTab the tab that is to be created.
	 */
	public LocComIndProdParameterPanel(
			CtrInformationPanel infoPanelCtr,
			EBenchmarkTab eBenchmarkTab) {
		super(null, eBenchmarkTab);
		this.eBenchmarkTab = eBenchmarkTab;
		
		// Test which EBenchmarkTab was handed over.
		if(eBenchmarkTab == EBenchmarkTab.LOCATION_PARAMETERS) {
			this.setName("Standorte");
		} else if(eBenchmarkTab == EBenchmarkTab.COMPANY_PARAMETERS) {
			this.setName("Unternehmen");
		} else if(eBenchmarkTab == EBenchmarkTab.INDUSTRY_PARAMETERS) {
			this.setName("Branchen");
		} else if(eBenchmarkTab == EBenchmarkTab.PRODUCT_PARAMETERS) {
			this.setName("Produkte");
		}
		
		this.setActive(true);
		init();
		drawSettings();
	}
	
	/**
	 * To set the panel's initial conditions.
	 */
	private void init() {
		settings = new JPanel();
		paramPanel = new JPanel();
		paramPanel.setLayout(new MigLayout());
		parameterPanels = new ArrayList<JPanel>();
		nameTextFields = new ArrayList<AlphaTextField>();
		
		this.setLayout(new MigLayout());
		this.add(settings, "wrap");
		this.add(paramPanel);
		this.add(logo, "aligny top");
	}
	
	
	/**
	 * To draw the settings panel of this {@code LocationParameterPanel}.
	 */
	private void drawSettings() {
		
		// Add JLabel depending on the EBenchmarkTab.
		if(eBenchmarkTab == EBenchmarkTab.LOCATION_PARAMETERS) {
			settings.add(new JLabel("Anzahl der Standorte festlegen: "));
		} else if(eBenchmarkTab == EBenchmarkTab.COMPANY_PARAMETERS) {
			settings.add(new JLabel("Anzahl der Unternehmen festlegen: "));
		} else if(eBenchmarkTab == EBenchmarkTab.INDUSTRY_PARAMETERS) {
			settings.add(new JLabel("Anzahl der Branchen festlegen: "));
		} else if(eBenchmarkTab == EBenchmarkTab.PRODUCT_PARAMETERS) {
			settings.add(new JLabel("Anzahl der Produkte festlegen: "));
		}
		
		btnAdd = new JButton("+");
		btnRemove = new JButton("-");
		
		settings.add(btnAdd);
		settings.add(btnRemove);
	}
	
	/**
	 * To add a parameter set to this panel.
	 * @param borderTitle the parameters' description.
	 * @param key the identifying key of the location, company or industrial 
	 * sector whose parameter panel is created.
	 * @return The newly created text fields.
	 */
	public List<JTextField> addParameterSet(
			String borderTitle,
			String key) {
		List<JTextField> newTextFields = new ArrayList<JTextField>();
		JPanel parameterPanel = new JPanel();
    	parameterPanel.setLayout(new MigLayout("wrap 2"));
    
		// Add name text field.
    	if(eBenchmarkTab == EBenchmarkTab.LOCATION_PARAMETERS) {
    		parameterPanel.add(new JLabel("Name des Standorts"));
		} else if(eBenchmarkTab == EBenchmarkTab.COMPANY_PARAMETERS) {
			parameterPanel.add(new JLabel("Name des Unternehmens"));
		} else if(eBenchmarkTab == EBenchmarkTab.INDUSTRY_PARAMETERS) {
			parameterPanel.add(new JLabel("Name der Branche"));
		} else if(eBenchmarkTab == EBenchmarkTab.PRODUCT_PARAMETERS) {
			parameterPanel.add(new JLabel("Name des Produkts"));
		}
    	AlphaTextField alphaTF = new AlphaTextField(key);
    	alphaTF.setText(borderTitle);    	
    	alphaTF.setCaretPosition(0);    	
    	parameterPanel.add(alphaTF);
    	nameTextFields.add(alphaTF);
    	newTextFields.add(alphaTF);
    	
    	parameterPanel.setBorder(BorderFactory.createTitledBorder(borderTitle));
    	parameterPanels.add(parameterPanel);
    	paramPanel.add(parameterPanel, "wrap");
    	
    	return newTextFields;
   }
	
	/**
	 * To remove a parameter set from this panel.
	 */
	public void removeParameterPanel() {
		int panelsCount = parameterPanels.size() - 1;
		List<AlphaTextField> removedAlphaTextFields = 
				new ArrayList<AlphaTextField>();
		
		if(panelsCount > 0) {			
			JPanel panelToRemove = parameterPanels.get(panelsCount);
			paramPanel.remove(panelToRemove);
			
			for(Component component : panelToRemove.getComponents()) {
				if (component instanceof AlphaTextField) {
					removedAlphaTextFields.add((AlphaTextField) component);
				}
			}
			nameTextFields.removeAll(removedAlphaTextFields);
			parameterPanels.remove(panelsCount);
			this.revalidate();
		}
	}
	
	/**
	 * To set a new text for a bordered parameter panel.
	 * @param position the position of the parameter panel within the panel.
	 * @param text the new text.
	 */
	public void setBorderText(int position, String text) {
		parameterPanels.get(position).setBorder(
				BorderFactory.createTitledBorder(text));
		this.revalidate();
	}
	
	@Override
	public void update() {		
				
		if(getErrorStatus()) {
			// To disable the buttons.
			btnAdd.setEnabled(false);
			btnRemove.setEnabled(false);
			
			// To disable all name text fields because of wrong input.
			for(AlphaTextField txtField : nameTextFields) {
				if(!txtField.getErrorStatus()) {
					txtField.setEnabled(false);
				}
			}
			
		} else {
			// To enable the buttons after the input was corrected.
			if(!btnAdd.isEnabled()) {
				btnAdd.setEnabled(true);
				btnRemove.setEnabled(true);
			}
			// To enable all disabled name text fields when wrong input was 
			// corrected.
			for(AlphaTextField txtField : nameTextFields) {
				if(!txtField.isEnabled()) {
					txtField.setEnabled(true);
				}
			}
		}
	}
	
	/**
	 * To get the name text fields.
	 * @return The text fields.
	 */
	public List<AlphaTextField> getNameTextFields() {
		return nameTextFields;
	}
	
	/**
	 * To get the "plus" button of this panel.
	 * @return The button.
	 */
	public JButton getAddButton() {
		return btnAdd;
	}
	
	/**
	 * To get the "subtract" button of this panel.
	 * @return The button.
	 */
	public JButton getRemoveButton() {
		return btnRemove;
	}
}
